package com.gskj.eduorder.service.impl;

import com.gskj.commonutils.OrderNoUtil;
import com.gskj.commonutils.orderVO.CourseWebVOOrder;
import com.gskj.commonutils.orderVO.UcenterMemberOrder;
import com.gskj.eduorder.client.EduClient;
import com.gskj.eduorder.client.UcenterClient;
import com.gskj.eduorder.entity.Order;
import com.gskj.eduorder.mapper.OrderMapper;
import com.gskj.eduorder.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Connor
 * @since 2020-08-17
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {


    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String createOrder(String courseId, String memberId) {

        // 1. 用过远程调用、获取会员信息、课程信息

        // edu-service
        CourseWebVOOrder course = eduClient.getCousrseInfoOrder(courseId);
        // ucenter-service
        UcenterMemberOrder member = ucenterClient.getMemberById(memberId);

        // 2.开始生成订单
        Order order = new Order();
        // 3. 将课程、会员的信息设置到 order，在进行添加
        // 订单号
        String orderNo = OrderNoUtil.getOrderNo();
        order.setOrderNo(orderNo);

        order.setCourseId(courseId); // 课程id
        order.setCourseTitle(course.getTitle()); // 课程名称
        order.setCourseCover(course.getCover()); // 课程封面
        order.setTeacherName(course.getTeacherName()); // 讲师名称
        order.setTotalFee(course.getPrice()); // 课程金额
        order.setMemberId(member.getId()); // 会员id
        order.setMobile(member.getMobile()); // 会员 手机号
        order.setNickname(member.getNickname()); // 会员昵称
        order.setStatus(0);// 默认支付状态  未支付
        order.setPayType(1); // 默认为 微信支付(1)

        // 4. 添加到数据库
        baseMapper.insert(order);
        // 5. 返回 订单号
        return orderNo;
    }
}
